package entity

import "shop/utils/convertutil"

const (
	TableNameSysUser = "sys_user"
	TableNameSysRole = "sys_role"
)

type SysUser struct {
	Id            int64  `json:"id" gorm:"column:id;primary_key"`
	ShopId        int    `json:"shopId" gorm:"column:shop_id"` // 店铺id
	Phone         string `json:"phone"`
	Username      string `json:"username"`
	Password      string `json:"password"`
	Salt          string `json:"salt"`
	RoleIdListStr string `json:"-" gorm:"column:role_id_list"` // 角色列表，英文逗号隔开
	Status        int    `json:"status"`                       // 账号状态，1：正常，2：冻结，3：禁用
	Avatar        string `json:"avatar"`

	RoleIdList    []int                  `json:"roleIdList" gorm:"-"`
	RoleNames     string                 `json:"roleNames" gorm:"-"`
	PermissionSet map[string]interface{} `json:"permissionSet" gorm:"-"` // 权限集合
}

func (SysUser) TableName() string {
	return TableNameSysUser
}

func (user *SysUser) RoleListToStr() {
	if user.RoleIdList == nil {
		user.RoleIdList = make([]int, 0, 0)
	}
	user.RoleIdListStr = convertutil.ToJsonString(user.RoleIdList)
}

func (user *SysUser) ParseRoleList() {
	user.RoleIdList = make([]int, 0, 0)
	convertutil.ParseJson(user.RoleIdListStr, &user.RoleIdList)
}

type SysRole struct {
	Id          int    `json:"id" gorm:"column:id;primary_key"`
	Name        string `json:"name"`
	Permissions string `json:"permissions"` // 权限列表
	Description string `json:"description"`

	PermissionList []string `json:"permissionList" gorm:"-"`
}

func (SysRole) TableName() string {
	return TableNameSysRole
}

func (role *SysRole) PermissionToStr() {
	if role.PermissionList == nil {
		role.PermissionList = make([]string, 0, 0)
	}
	role.Permissions = convertutil.ToJsonString(role.PermissionList)
}

func (role *SysRole) ParsePermissionList() {
	role.PermissionList = make([]string, 0, 0)
	convertutil.ParseJson(role.Permissions, &role.PermissionList)
}
